package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.reflection.ClassConvertUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyHuiWan;
import com.ts.api.http.org.entity.dataexchange.OrgDataExchangeHuiWan;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

/**
 * 安心分期-汇万金服-银川全量20api
 * https://hw-bam.kaifa.fun/api.html?sourceId=id&baseUrl=aHR0cHM6Ly9ody1iYW0ua2FpZmEuZnVuL2FwaS8=
 * @ Author 李源聪
 * @ copy master分支文件ApiOrgHuiWan
 * copy日期 2025-05-07
 */
@ApiPlan(productId = "184", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5HuiWan implements ApiOrgPhoneMd5ConvertHandler {
    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        OrgDataExchangeHuiWan orgDataExchangeHaiChuang = new OrgDataExchangeHuiWan();
        orgDataExchangeHaiChuang.setMd5Phone(apiDataExchangeParameter.getPhoneMd5());
        return JsonUtils.toJson(orgDataExchangeHaiChuang);
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        OrgApplyHuiWan orgApplyHaiChuangData = new OrgApplyHuiWan();
        OrgApplyHuiWan.Client client = orgApplyHaiChuangData.new Client();
        //  拓展判断信息
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);

        //  年龄
        client.setAge(apiFieldConverter.getAge());

        //  拥有车产数量
        client.setCarCount(apiFieldConverter.hasCarInfoToNumber());
        //  城市
        client.setCity(apiApplyParameter.getCity());
        //  逾期
        if (ValidParam.isNotEmpty(apiApplyParameter.getOverdue())) {
            Integer text = ChooseEq.create()
                    .when(apiApplyParameter.getOverdue(), "2", 1)
                    .when(apiApplyParameter.getOverdue(), "3", 7)
                    .end();
            client.setCreditCondition(text);
        }
        //  月收入
        client.setEarningMonth(apiFieldConverter.getMonthlySalaryNumber());
        //  学历
        if (ValidParam.isNotEmpty(apiApplyParameter.getEducationAttainment())){
            Integer text = ChooseEq.create()
                    .when(apiApplyParameter.getEducationAttainment(), "1", 0)
                    .when(apiApplyParameter.getEducationAttainment(), "2", 0)
                    .when(apiApplyParameter.getEducationAttainment(), "3", 1)
                    .when(apiApplyParameter.getEducationAttainment(), "4", 2)
                    .when(apiApplyParameter.getEducationAttainment(), "5", 3)
                    .end();
            client.setEducation(text);
        }
        //  性别
        String sex = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "0", "男")
                .when(apiApplyParameter.getSex(), "1", "女")
                .end("未知");
        client.setGender(sex);
        //  房产数量
        client.setHouseCount(apiFieldConverter.hasRealEstateInfoToNumber());
        //  身份证
        client.setIdCard(apiApplyParameter.getIdcard());
        //  职业
        if (ValidParam.isNotEmpty(apiApplyParameter.getCareer())){
            Integer text = ChooseEq.create()
                    .when(apiApplyParameter.getCareer(), "1", 2)
                    .when(apiApplyParameter.getCareer(), "2", 1)
                    .when(apiApplyParameter.getCareer(), "3", 4)
                    .when(apiApplyParameter.getCareer(), "4", 3)
                    .when(apiApplyParameter.getCareer(), "5", 5)
                    .end();
            client.setJobType(text);
        }
        //  姓名
        client.setName(apiApplyParameter.getUserName());
        //  打卡方式
        if (ValidParam.isNotEmpty(apiApplyParameter.getSalaryPayMethod())) {
            Integer text = ChooseEq.create()
                    .when(apiApplyParameter.getSalaryPayMethod(), "1", 1)
                    .when(apiApplyParameter.getSalaryPayMethod(), "2", 2)
                    .when(apiApplyParameter.getSalaryPayMethod(), "3", 3)
                    .end();
            client.setPayoffWay(text);
        }
        //  手机号
        client.setPhone(apiApplyParameter.getPhone());
        //  渠道
        client.setSource("安心分期");
        //  客户类型
        String career = apiApplyParameter.getCareer();
        if (ValidParam.isEmpty(career)) {
            //  无数据为个人用户
            client.setType(0);
        } else {
            Integer text = ChooseEq.create()
                    //  如果是企业就为企业用户
                    .when(career, "3", 1)
                    //  个人用户
                    .end(0);
            client.setType(text);
        }
        //  贷款金额
        client.setWantAmount(apiFieldConverter.getLoanAmountInt());
        //  工龄
        if (ValidParam.isNotEmpty(apiApplyParameter.getYearsService())){
            client.setWorkYear(ClassConvertUtils.toInt(apiApplyParameter.getYearsService()));
        }
        //备注
        if (ValidParam.isNotEmpty(apiFieldConverter.getExtendInfo())) {
            StringBuilder stringBuilder = new StringBuilder();
            //  信用记录-逾期记录
            if (ValidParam.isNotEmpty(apiApplyParameter.getOverdue())) {
                String text = ChooseEq.create()
                        .when(apiApplyParameter.getOverdue(), "1", "无逾期")
                        .when(apiApplyParameter.getOverdue(), "2", "有逾期")
                        .when(apiApplyParameter.getOverdue(), "3", "一年以内无逾期")
                        .end();
                stringBuilder.append("信用记录：").append(text).append(";").append("  ");
            }

            //  芝麻分
            if (ValidParam.isNotEmpty(apiApplyParameter.getSesameCredit())) {
                String text = ChooseEq.create()
                        .when(apiApplyParameter.getSesameCredit(), "1", "600分以下")
                        .when(apiApplyParameter.getSesameCredit(), "2", "600~649分")
                        .when(apiApplyParameter.getSesameCredit(), "3", "650~699分")
                        .when(apiApplyParameter.getSesameCredit(), "4", "700分以上")
                        .end();
                stringBuilder.append("芝麻分：").append(text).append(";").append("  ");
            }

            //  职业
            if (ValidParam.isNotEmpty(apiApplyParameter.getCareer())) {
                String text = ChooseEq.create()
                        .when(apiApplyParameter.getCareer(), "1", "上班族")
                        .when(apiApplyParameter.getCareer(), "2", "自由职业")
                        .when(apiApplyParameter.getCareer(), "3", "企业主（有营业执照）")
                        .when(apiApplyParameter.getCareer(), "4", "公务员或事业单位")
                        .end();
                stringBuilder.append("职业：").append(text).append(";").append("  ");
            }

            //  学历
            if (ValidParam.isNotEmpty(apiApplyParameter.getEducationAttainment())) {
                String text = ChooseEq.create()
                        .when(apiApplyParameter.getEducationAttainment(), "1", "初中及以下")
                        .when(apiApplyParameter.getEducationAttainment(), "2", "中专")
                        .when(apiApplyParameter.getEducationAttainment(), "3", "高中")
                        .when(apiApplyParameter.getEducationAttainment(), "4", "大专")
                        .when(apiApplyParameter.getEducationAttainment(), "5", "本科")
                        .when(apiApplyParameter.getEducationAttainment(), "6", "研究生及以上")
                        .end();
                stringBuilder.append("学历：").append(text).append(";").append("  ");
            }

            //  收入形式
            if (ValidParam.isNotEmpty(apiApplyParameter.getSalaryPayMethod())) {
                String text = ChooseEq.create()
                        .when(apiApplyParameter.getSalaryPayMethod(), "1", "银行代发打卡")
                        .when(apiApplyParameter.getSalaryPayMethod(), "2", "现金收入")
                        .when(apiApplyParameter.getSalaryPayMethod(), "3", "个人转账")
                        .end();
                stringBuilder.append("收入形式：").append(text).append(";").append("  ");
            }

            //  客户类型
            String work = apiApplyParameter.getCareer();
            if (ValidParam.isEmpty(work)) {
                //  无数据为个人用户
                stringBuilder.append("客户类型：").append("个人").append(";").append("  ");
            } else {
                String text = ChooseEq.create()
                        //  如果是企业就为企业用户
                        .when(work, "3", "企业")
                        //  个人用户
                        .end("个人");
                stringBuilder.append("客户类型：").append(text).append(";").append("  ");
            }

            //  房产情况
            if (ValidParam.isNotEmpty(apiApplyParameter.getRealEstateInfo())) {
                String text = ChooseEq.create()
                        .when(apiApplyParameter.getRealEstateInfo(), "1", "无")
                        .end("有");
                stringBuilder.append("房产情况：").append(text).append(";").append("  ");
            }

            //  车辆情况
            if (ValidParam.isNotEmpty(apiApplyParameter.getCarInfo())) {
                String text = ChooseEq.create()
                        .when(apiApplyParameter.getCarInfo(), "1", "无")
                        .end("有");
                stringBuilder.append("车辆情况：").append(text).append(";").append("  ");
            }

            //  保单
            if (ValidParam.isNotEmpty(apiApplyParameter.getInsurancePolicy())) {
                String text = ChooseEq.create()
                        .when(apiApplyParameter.getInsurancePolicy(), "1", "无")
                        .end("有");
                stringBuilder.append("保单：").append(text).append(";").append("  ");
            }

            //  社保
            if (ValidParam.isNotEmpty(apiApplyParameter.getSocialSecurity())) {
                String text = ChooseEq.create()
                        .when(apiApplyParameter.getSocialSecurity(), "1", "无")
                        .end("有");
                stringBuilder.append("社保：").append(text).append(";").append("  ");
            }

            //  公积金
            if (ValidParam.isNotEmpty(apiApplyParameter.getProvidentFund())) {
                String text = ChooseEq.create()
                        .when(apiApplyParameter.getProvidentFund(), "1", "无")
                        .end("有");
                stringBuilder.append("公积金：").append(text).append(";").append("  ");
            }

            //  银行代发
            if (ValidParam.isNotEmpty(apiApplyParameter.getSalaryPayMethod())) {
                String text = ChooseEq.create()
                        .when(apiApplyParameter.getSalaryPayMethod(), "1", "是")
                        .end("否");
                stringBuilder.append("银行代发：").append(text).append(";").append("  ");
            }
            client.setRemark(stringBuilder.toString());
        }
        return JsonUtils.toJson(client);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
